package com.heatup.apt.management.pay;

import java.util.HashMap;
import java.util.Map;

import com.heatup.apt.common.util.TemplateMsgUtil;

import net.sf.json.JSONObject;

/**
 * 二维码生成
 * @author jixiangyun
 * 2016年11月18日 上午10:40:25
 */
public class QrCodeUtil {
	private static String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
	
	public void QrCodeEvent(String url,String ticket){
		String xml = "<xml>"+
						"<ToUserName>chdd1002512666</ToUserName>"+
						"<FromUserName>o0r3rwTr11nZygsdVYWKnyyFJg-Y</FromUserName>"+
						"<CreateTime>123456789</CreateTime>"+
						"<MsgType><![CDATA[event]]></MsgType>"+
						"<Event><![CDATA[SCAN]]></Event>"+
						"<EventKey><![CDATA[SCENE_VALUE]]></EventKey>"+
						"<Ticket><![CDATA[gQHu7zoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2ZEck9NWUxrRkd4dFVYSm9XQllXAAIEu24uWAMEAAAAAA==]]></Ticket>"+
						"</xml>";
		
	}
	
	/**
	 * 创建永久性的二维码
	 * 每个微信公众号只能有十万张
	 * 所有要使用该方法时要慎重
	 *
	 * @param access_token
	 * @param code 二维码中的参数 最大值为 100000
	 * @return
	 */
	public static Map createdQRCode(String access_token,String code){
		Map<String,Object> map = new HashMap<String,Object>();
		String urls = url + access_token;
		String canshu = "{\"expire_seconds\": 604800, \"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\":"+code+"}}}";
		try {
			JSONObject json=JSONObject.fromObject(canshu);
			JSONObject jsonResult = TemplateMsgUtil.httpsRequestToJsonObject(urls, "POST", canshu);
			String ticket = jsonResult.getString("ticket");
			String qr_url = jsonResult.getString("url");
			map.put("ticket", ticket);
			map.put("url", qr_url);
			System.out.println(jsonResult);
		} catch (Exception e) {
			map.put("ticket", "");
			map.put("url", "");
			
			// TODO: handle exception
		}
		
		return map;
	}
	
	/**
	 * 生成带参数的临时二维码
	 * @param access_token
	 * @param code 场景的参数字符串 最大长度为64位
	 * @return
	 */
	public static Map createQRSCENE(String access_token,String code){
		Map<String,Object> map = new HashMap<String,Object>();
		String urls = url + access_token;
		String canshu = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\":\""+code.trim()+"\"}}}";
		try {
			JSONObject json=JSONObject.fromObject(canshu);
			JSONObject jsonResult = TemplateMsgUtil.httpsRequestToJsonObject(urls, "POST", canshu);
			String ticket = jsonResult.getString("ticket");
			String qr_url = jsonResult.getString("url");
			map.put("ticket", ticket);
			map.put("url", qr_url);
			map.put("imgUrl", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket);
			System.out.println(jsonResult);
		} catch (Exception e) {
			map.put("ticket", "");
			map.put("url", "");
			// TODO: handle exception
		}
		
		return map;
	}
	
	public static void main(String[] args) {
		/*String qrscene = "qrscene_123";
		String str = qrscene.split("_")[1];
		System.out.println(str);*/
		/*String canshu = "{\"expire_seconds\": 604800, \"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 125}}}";
		JSONObject json=JSONObject.fromObject(canshu);
		JSONObject jsonResult = TemplateMsgUtil.httpsRequestToJsonObject(url, "POST", canshu);
		String ticket = jsonResult.getString("ticket");
		String url = jsonResult.getString("url");
		System.out.println(jsonResult);*/
		String code = 1+"#"+"o0r3rwcucLPBV1kKvF035g_Ab0Y0";
		Map<String,String> map = createQRSCENE("Dz94V_hgsVP3uPlcewQpBG6nsA3P9Nw33r_k8tH8GMIGfApq_YbqIilnmWwAoeHtVfBeQlZwvHdV2rSxhdnd1tAG6TvSoX1AzsK1JzYM627EWnfTNBo2bvjCOdXo1WDOLIDbAAAFCX",code);
		System.out.println("imgUrl:"+map.get("imgUrl"));
		
	}
	
}
